﻿Public Class Reference
    Public Ref As Integer
    Public lstRefClt As List(Of Partager.clt)
    Public lstRefAdv As List(Of Partager.clt)
    Public dateInscriptionBur As Date

    Public Sub New()
        Me.lstRefClt = New List(Of Partager.clt)
        Me.lstRefAdv = New List(Of Partager.clt)
    End Sub

    Public Sub New(ByVal ref As Integer, ByVal dateInscriptionBur As Date)
        Me.Ref = ref
        Me.dateInscriptionBur = dateInscriptionBur
        Me.lstRefClt = New List(Of Partager.clt)
        Me.lstRefAdv = New List(Of Partager.clt)
    End Sub

    Public Function ajouterClient(ByVal cltId As Integer) As String
        Dim str As String = ""
        Try
            Dim cmd As New SqlClient.SqlCommand("insert into refClt values(" & Me.Ref & "," & cltId & ")", Partager.con)

            Partager.open()
            cmd.ExecuteNonQuery()
            Partager.close()

        Catch sqlEx As SqlClient.SqlException
            str = sqlEx.Message
        End Try
        Return str
    End Function

    Public Function ajouterAdv(ByVal advId As Integer) As String
        Dim str As String = ""
        Try
            Dim cmd As New SqlClient.SqlCommand("insert into refAdv values(" & Me.Ref & "," & advId & ")", Partager.con)

            Partager.open()
            cmd.ExecuteNonQuery()
            Partager.close()

        Catch sqlEx As SqlClient.SqlException
            str = sqlEx.Message
        End Try
        Return str
    End Function


    Public Sub suprimmer_Client(ByVal id As Integer)
        Dim cmd As New SqlClient.SqlCommand("delete refClt where idRef = " & Me.Ref & " And idClt =" & id, Partager.con)
        Partager.open()
        cmd.ExecuteNonQuery()
        Partager.close()
        'remplir_lstRefClt()
    End Sub

    Public Sub suprimmer_Adversaire(ByVal id As Integer)
        Dim cmd As New SqlClient.SqlCommand("delete refAdv where idRef = " & Me.Ref & " And idAdv =" & id, Partager.con)
        Partager.open()
        cmd.ExecuteNonQuery()
        Partager.close()
        'remplir_lstRefAdv()
    End Sub

    Public Sub remplir_lstRefClt()
        Me.lstRefClt.Clear()
        Dim cmd As New SqlClient.SqlCommand("select Id,Nom,Prenom,Nom_entreprise,Type_Client from Clients " & _
            "where Id in( select idClt from refClt where idRef = " & Me.Ref & ")", Partager.con)
        Partager.open()
        Partager.dr = cmd.ExecuteReader
        If Partager.dr.HasRows Then
        While Partager.dr.Read
            Dim khab As New Partager.clt
            khab.id = Partager.dr("Id")

            If Partager.dr("Type_Client") = "ذاتي" Then
                khab.nom = Partager.dr("Nom") & "  " & Partager.dr("Prenom")
            ElseIf Partager.dr("Type_Client") = "معنوي" Then
                khab.nom = Partager.dr("Nom_entreprise")
            End If

            Me.lstRefClt.Add(khab)
            End While
        End If
        Partager.close()
        Partager.dr.Close()
    End Sub

    Public Sub remplir_lstRefAdv()
        Me.lstRefAdv.Clear()
        Dim cmd As New SqlClient.SqlCommand("select Id,Nom,Prenom,Nom_entreprise,Type_Adversaire from Adversaire " & _
            "where Id in( select idAdv from refAdv where idRef = " & Me.Ref & ")", Partager.con)
        Partager.open()
        Partager.dr = cmd.ExecuteReader
        If Partager.dr.HasRows Then
        While Partager.dr.Read
            Dim khab As New Partager.clt
            khab.id = Partager.dr("Id")

            If Partager.dr("Type_Adversaire") = "ذاتي" Then
                khab.nom = Partager.dr("Nom") & "  " & Partager.dr("Prenom")
            ElseIf Partager.dr("Type_Adversaire") = "معنوي" Then
                khab.nom = Partager.dr("Nom_entreprise")
            End If

            Me.lstRefAdv.Add(khab)
            End While
        End If
        Partager.close()
        Partager.dr.Close()
    End Sub

    'exemple pour réaliser les même fonction

    'Public Sub remplir_List_avoca_partenaire()
    '    Me.List_avoca_partenaire.Clear()
    '    Dim cmd As New SqlClient.SqlCommand("select * from Dossiers_Avocat_Partonaire where  Id_Dossier =" & Me.id, Partager.con)
    '    Partager.open()
    '    Partager.dr = cmd.ExecuteReader
    '    While Partager.dr.Read
    '        Dim khab As New Dossier_Avocat_Partenaire(Partager.dr("Id_Dossier"), Partager.dr("Id_Avocat_Partonaire"), Partager.dr("nomme"))
    '        Me.List_avoca_partenaire.Add(khab)
    '    End While
    '    Partager.close()
    'End Sub

    'Public Sub suprimmer_avoca_partenaire(ByVal id As Integer)
    '    Dim cmd As New SqlClient.SqlCommand("delete  Dossiers_Avocat_Partonaire where Id_Avocat_Partonaire =" & id, Partager.con)

    '    Partager.open()
    '    cmd.ExecuteNonQuery()
    '    Partager.Close()

    '    Me.remplir_List_avoca_partenaire()
    'End Sub
End Class
